package com.mjk.manager.amis.core.controller;


import cn.hutool.core.util.StrUtil;
import com.mjk.common.base.error.BeeError;
import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.core.jdbc.service.plus.idbservice.BeeDBService;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;
import com.mjk.common.util.ObjectUtils;
import com.mjk.common.util.TreeUtils;
import com.mjk.manager.amis.core.dto.OptionDTO;
import com.mjk.manager.amis.core.dto.SessionDetails;
import com.mjk.manager.amis.core.model.PlatSys;
import com.mjk.manager.amis.core.model.SysDept;
import com.mjk.manager.amis.core.utils.SessionHolder;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/mamis/sys/dept")
@RestController
@Slf4j
public class SysDeptController {
    @Autowired
    SessionHolder contextService;

    @Autowired
    BeeDBService beeDBService;


    @GetMapping(value = {"/option/list", "list"})
    public BeeResponse getDeptOptionList(final HttpServletRequest request) {
        String curSysId = contextService.getSystem(request).getId();
        if(ObjectUtils.isEmpty(curSysId)){
            return BeeError.TOKEN_IS_EXPRIE.tranceError();
        }
        // 查询数据库
        BeeQueryWrapper wrapper = new BeeQueryWrapper(new SysDept());

        final List<SysDept> deptList = beeDBService.findManyByWrapper(wrapper);
        final List<SysDept> deptTree = TreeUtils.list2Tree(deptList, "0", "id", "PID", "children", false);


        List<Map<String, Object>> rootList = new ArrayList<>();

        // 虚拟名为系统名称的根节点
        Map<String, Object> root = new HashMap<String, Object>();
        rootList.add(root);
        BeeQueryWrapper wrapper1 = new BeeQueryWrapper(new PlatSys());
        wrapper1.eq(PlatSys::getId, curSysId);
        List<PlatSys> sysList = beeDBService.findManyByWrapper(wrapper1);
        root.put("label", sysList.get(0).getNAME());
        root.put("id",sysList.get(0).getId());
        root.put("value", "root");
        root.put("children", deptTree);
        root.put("creatable", true);
        root.put("editable", false);
        root.put("removable", false);

        // 返回结果集
        return BeeError.BEE_SUC.tranceError(rootList);
    }

    @PostMapping("/option")
    public BeeResponse saveDeptOption(final HttpServletRequest request, @RequestBody OptionDTO obj) {
        final SessionDetails session = contextService.getSession(request);
        if(session == null){
            return BeeError.TOKEN_IS_EXPRIE.tranceError();
        }
        SysDept entity = new SysDept();

        // 提取数据信息
        entity.setNAME(obj.getLabel());
        if (obj.getParent() != null) {
            String value = obj.getParent().getValue();
            if (StrUtil.equals(value, "root")) {
                value = null;
            }
            entity.setPID(value);
        }

        // 设置更新人员
        entity.setUPDATED_BY(session.getCurUser().getId());

        int ret;
        if (StrUtil.isNotBlank(obj.getValue())) {
            // 更新
            BeeQueryWrapper wrapper = new BeeQueryWrapper(entity);
            wrapper.eq(SysDept::getId, obj.getValue());
            ret = beeDBService.update(wrapper);
        } else {
            // 插入

            // 设置创建人员
            entity.setCREATED_BY(session.getCurUser().getId());

            //entity.setOWN_SYS_ID(contextService.getSystem(request).getId());

            ret = beeDBService.save(entity);
        }

        // 返回结果集
        return ret > 0 ? BeeError.BEE_SUC.tranceError() : BeeError.BEE_FAILD.tranceError();
    }

    @DeleteMapping("/option/{id}")
    public BeeResponse delDeptOption(final HttpServletRequest request, @PathVariable("id") String id) {
        SysDept dept = new SysDept();
        dept.setDELETED(true);
        BeeQueryWrapper wrapper = new BeeQueryWrapper(dept);
        wrapper.eq(SysDept::getId, id);
        int ret = beeDBService.update(wrapper);

        return ret > 0 ? BeeError.BEE_SUC.tranceError() : BeeError.BEE_FAILD.tranceError();
    }

}
